<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/6
 * Time: 9:27
 */

namespace app\api\controller\shop;


use app\admin\model\ShopRelationCategory;
use app\api\model\Config;
use app\api\model\Product;
use app\api\model\ShopGroup;
use app\lib\exception\NotFoundException;
use app\Validate\IdMustPositive;
use app\Validate\PageMustValidate;
use think\Controller;
use think\Request;


class ShopList extends Controller
{
    /**
     * 线上商家列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     */
    public function onlineShop(Request $request){
        (new PageMustValidate())->goCheck();
        $post = $request->post();
        $where =[
            ['use_status','eq',1],
            ['status','eq',2],
            ['type','eq',1],
        ];
        $where['page'] = $post['page'];
        $dataList = (new \app\api\model\Shop())->wechatListTotalWith("pname,cname,aname",$where,"*","order_product_number desc",$post['page_number']);
        $dataList = (new \app\api\model\Shop())->action_data($dataList);
        return writeJson(200,$dataList);
    }
    /**
     * 品牌店列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     */
    public function brandShop(Request $request){
        (new PageMustValidate())->goCheck();
        $post = $request->post();
        $where =[
            ['use_status','eq',1],
            ['status','eq',2],
            ['type','eq',2],
        ];
        $where['page'] = $post['page'];
        $dataList = (new \app\api\model\Shop())->wechatListTotalWith("pname,cname,aname",$where,"*","order_product_number desc",$post['page_number']);
        $dataList = (new \app\api\model\Shop())->action_data($dataList);
        return writeJson(200, $dataList);
    }

    /**
     * 根据商家id 查看商家详情
     * @param Request $request
     * @return \think\response\Json
     * @throws NotFoundException
     * @throws \app\lib\exception\ParameterException
     */
    public function shopDetail(Request $request){
        (new IdMustPositive())->goCheck();
        $post = $request->post();
        $where =[
            ['id','eq',$post['id']],
            ['use_status','eq',1]
        ];
        $detail = (new \app\api\model\Shop())->findOneWith("pname,cname,aname",$where);
        if(!$detail){
            throw new NotFoundException(['msg'=>'商家不存在']);
        }
        $detail['logo']  = imgUrlToAbsolute($detail['logo']);
        if(empty($detail['banner'])){
            $detail['banner'] = (new Config())->findOne([['k','eq','default_shop_banner']],'v');
        }
        $detail['banner'] = imgUrlToAbsolute($detail['banner']);
        $detail['category'] = (new ShopRelationCategory())->selectListWith("category_name",[['shop_id','eq',$post['id']]],"*","category_id");
        return writeJson(200,['detail'=>$detail]);
    }
    /**
     * 商家下的商品分组
     * @param Request $request
     * @return \think\response\Json
     * @throws NotFoundException
     * @throws \app\lib\exception\ParameterException
     */
    public function shopProductGroup(Request $request){
        (new IdMustPositive())->goCheck();
        (new PageMustValidate())->goCheck();
        $post = $request->post();
        $where =[
            ['uid','eq',$post['id']],
        ];
        $where['page'] = $post['page'];
        $dataList = (new ShopGroup())->wechatListTotal($where,"id,group_name","sort desc",$post['page_number']);
        return writeJson(200,$dataList);
    }
}